package com.codecorp.cortex_scan;

import android.app.Activity;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.TextView;

import com.codecorp.CortexDecoderLibrary;

import java.text.SimpleDateFormat;
import java.util.Date;
import com.example.ht380d4G_jar_demo.R;

public class InformationActivity extends Activity implements CortexDecoderLibrary.CortexDecoderLicenseCallback{

    private MyApplication mApplication;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mApplication = (MyApplication) getApplication();

        setContentView(R.layout.information);
        WebView wv = (WebView) findViewById(R.id.webview);
        TextView appVersion = (TextView) findViewById(R.id.app_version);
        PackageManager manager = getPackageManager();
        PackageInfo info = null;
        try {
            info = manager.getPackageInfo(getPackageName(), 0);
            appVersion.setText(info.versionName + " (" + info.versionCode + ")");
        } catch (PackageManager.NameNotFoundException e) {
            appVersion.setText("ERROR");
        }

        TextView sdkVersion = (TextView) findViewById(R.id.sdk_version);
        sdkVersion.setText(CortexDecoderLibrary.sharedObject(this).libraryVersion());
        TextView decoderVersion = (TextView) findViewById(R.id.decoder_version);
        decoderVersion.setText(CortexDecoderLibrary.sharedObject(this).decoderVersion());

        updateLicenseFields();

        wv.loadUrl("file:///android_asset/about.html");
    }

    @Override
    public void onResume() {
        super.onResume();
        mApplication.mCortexDecoderLibrary.setLicenseCallback(this);
        updateLicenseFields();
    }

    @Override
    public void onPause() {
        super.onPause();
        mApplication.mCortexDecoderLibrary.setLicenseCallback(null);
    }

    @Override
    public void licenseStatusCallback(CortexDecoderLibrary.LicenseStatusCode statusCode) {
        // update UI
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                updateLicenseFields();
            }
        });
    }

    private void updateLicenseFields() {
        if (mApplication.mCortexDecoderLibrary != null) {
            TextView licenseType = (TextView) findViewById(R.id.licenseType);
            licenseType.setText(mApplication.mCortexDecoderLibrary.getLicenseKeyValue(CortexDecoderLibrary.LicenseGetKeyValues.License_GetType));
            TextView licenseStatus = (TextView) findViewById(R.id.licenseStatus);
            licenseStatus.setText(mApplication.mCortexDecoderLibrary.getLicenseKeyValue(CortexDecoderLibrary.LicenseGetKeyValues.License_GetStatusCodeStr));
            TextView licenseLastSync = (TextView) findViewById(R.id.licenseLastSync);
            licenseLastSync.setText(mApplication.mCortexDecoderLibrary.getLicenseKeyValue(CortexDecoderLibrary.LicenseGetKeyValues.License_GetSyncTime));
            TextView licenseSyncDays = (TextView) findViewById(R.id.licenseSyncDays);
            String strSyncDays = mApplication.mCortexDecoderLibrary.getLicenseKeyValue(CortexDecoderLibrary.LicenseGetKeyValues.License_GetSyncDays) + " day(s)";
            licenseSyncDays.setText(strSyncDays);
            // licenseSyncDays.setText(mApplication.mCortexDecoderLibrary.getLicenseKeyValue(CortexDecoderLibrary.LicenseGetKeyValues.License_GetSyncDays));
            TextView androidID = (TextView) findViewById(R.id.androidID);
            androidID.setText(mApplication.mCortexDecoderLibrary.getLicenseKeyValue(CortexDecoderLibrary.LicenseGetKeyValues.License_GetDeviceID));
            TextView wifiMacAddr = (TextView) findViewById(R.id.wifiMacAddr);
            wifiMacAddr.setText(mApplication.mCortexDecoderLibrary.getLicenseKeyValue(CortexDecoderLibrary.LicenseGetKeyValues.License_GetWifiMacAddress));
            TextView currentDateTime = (TextView) findViewById(R.id.currentDateTime);
            SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss");
            String cdt = sdf.format(new Date());
            currentDateTime.setText(cdt);
        }
    }
}
